from haystack import indexes
from .models import SKU


# 继承的类必须这样写，（搜索下标，　能够下标）
class SKUIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    id = indexes.IntegerField(model_attr='id')
    name = indexes.CharField(model_attr='name')
    price = indexes.DecimalField(model_attr='price')
    default_image_url = indexes.CharField(model_attr='default_image_url')
    comments = indexes.IntegerField(model_attr='comments')

    def get_model(self):
        """返回建立索引的模型类"""
        return SKU

    def index_queryset(self, using=None):
        return self.get_model().objects.filter(is_launched=True)
    # 也可以return SKU.objects.filter(is_launched=True)，但是为了通用，就使用方法获取

